package agency.tango.materialintroscreen.animations;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;

import agency.tango.materialintroscreen.animations.translations.NoTranslation;

/**
 * @ProjectName: material-intro-screen-master
 * @Package: agency.tango.materialintroscreen.animations
 * @ClassName: ViewTranslationWrapper
 * @Description: 控件随着滑动距离变化显示的动画封装器
 * @CreateDate: 2021/4/12 16:04
 * @UpdateDate: 2021/4/12 16:04
 * @UpdateRemark: 暂无
 * @Version: 1.0
 */

public class ViewTranslationWrapper {
    private Component view;
    private IViewTranslation enterTranslation;
    private IViewTranslation exitTranslation;
    private IViewTranslation defaultTranslation;
    private AnimatorProperty errorAnimation;

    public ViewTranslationWrapper(Component view) {
        this.view = view;
        enterTranslation = new NoTranslation();
        exitTranslation = new NoTranslation();
        setErrorAnimation(null);
    }

    /**
     * Set translation after passing first slide
     *
     * @param enterTranslation new translation
     * @return ViewTranslationWrapper object
     */
    public ViewTranslationWrapper setEnterTranslation(IViewTranslation enterTranslation) {
        this.enterTranslation = enterTranslation;
        return this;
    }

    /**
     * Set translation after passing last slide
     *
     * @param exitTranslation new translation
     * @return ViewTranslationWrapper object
     */
    public ViewTranslationWrapper setExitTranslation(IViewTranslation exitTranslation) {
        this.exitTranslation = exitTranslation;
        return this;
    }

    /**
     * Set default translation
     *
     * @param defaultTranslation new translation
     * @return ViewTranslationWrapper object
     */
    public ViewTranslationWrapper setDefaultTranslation(IViewTranslation defaultTranslation) {
        this.defaultTranslation = defaultTranslation;
        return this;
    }

    /**
     * Set view on error animation
     *
     * @param errorAnimation new animation
     * @return ViewTranslationWrapper object
     */
    public ViewTranslationWrapper setErrorAnimation(AnimatorProperty errorAnimation) {
        if (errorAnimation != null) {
            this.errorAnimation = errorAnimation;
        }
        return this;
    }

    /**
     * 进场动画
     *
     * @param percentage 位移比
     */
    public void enterTranslate(float percentage) {
        enterTranslation.translate(view, percentage);
    }

    /**
     * 退场动画
     *
     * @param percentage 位移比
     */
    public void exitTranslate(float percentage) {
        exitTranslation.translate(view, percentage);
    }

    /**
     * 默认位移动画
     *
     * @param percentage 位移比
     */
    public void defaultTranslate(float percentage) {
        defaultTranslation.translate(view, percentage);
    }

    /**
     * 出错(无权限 未选中等)之后执行错误动画
     */
    public void error() {
        if (errorAnimation != null) {
            // 为了保证多次点击有效必须在动画结束后重置 否则只能点击一次
            errorAnimation.setStateChangedListener(new Animator.StateChangedListener() {
                @Override
                public void onStart(Animator animator) {
                }

                @Override
                public void onStop(Animator animator) {
                    errorAnimation.reset();
                }

                @Override
                public void onCancel(Animator animator) {
                    errorAnimation.reset();
                }

                @Override
                public void onEnd(Animator animator) {
                    errorAnimation.reset();
                }

                @Override
                public void onPause(Animator animator) {
                    errorAnimation.reset();
                }

                @Override
                public void onResume(Animator animator) {
                }
            });
            errorAnimation.start();
        }
    }
}